package dao.connectortodb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectorToDB {
	private DistributerProperties distributer_properties;
	private Connection connection = null;
	private static final String CONNECT = "The connection to data base is successfull";
	private static final String SQL_EXCEPTION = " Database access error";
	private static final String INSTANTIATION_EXCEPTION = "The specified class object cannot be instantiated";
	private static final String ILLEGALACCESS_EXCEPTION = "In the currently executing method does not have access to the definition of the specified class, field, method or constructor";
	private static final String CLASSNOTFOUND_EXCEPTION = "Sorry, class not found";
	private static ConnectorToDB CONNECTOR_TO_DB = null;

	private ConnectorToDB() {
		distributer_properties=new DistributerProperties();

	}

	public static ConnectorToDB getInstance() {
		if (CONNECTOR_TO_DB == null) {
			CONNECTOR_TO_DB = new ConnectorToDB();
		}
		return CONNECTOR_TO_DB;
	}

	public Connection getConnection() {
		try {
			Class.forName(distributer_properties.getDriverName()).newInstance();

			connection = DriverManager
					.getConnection(distributer_properties.getUrlDriver(), distributer_properties.getUser(),distributer_properties.getPassword());
			System.out.println(CONNECT);

		}

		catch (SQLException sqle) {
			printException(SQL_EXCEPTION);
		} catch (InstantiationException ie) {
			printException(INSTANTIATION_EXCEPTION);
		} catch (IllegalAccessException iae) {
			printException(ILLEGALACCESS_EXCEPTION);
		} catch (ClassNotFoundException cnfe) {
			printException(CLASSNOTFOUND_EXCEPTION);
		}

		return connection;

	}

	private void printException(String message) {
		System.out.println(message);
	}

}
